#ifndef __BSP_MOTOR_H__
#define __BSP_MOTOR_H__

#include "Arduino.h"
#include <HardwareSerial.h>

typedef enum Speed          
{
    STOP = 0,               //不要设置STOP      
   
    LOWSPEED = 30 ,         //速度最大为240 最小为0 
    MIDSPEED = 80 ,
    HIGHSPEED = 130 ,
    FASTSPEED = 170 , 
    EXHIGHSPEED = 210,
    FULLSPEED = 240 , 

    L_R_CALIB = 3,          //左右轮速度校准值 正值增大右轮速度 负值减小右轮速度
 
    LOW_OFFSET = 10 ,       //低速差速值  正值
    MID_OFFSET = 20 ,       //中速差速值  正值
    HIGH_OFFSET = 30,       //高速差速值  正值
    FAST_OFFSET = 40,       //超高差速值  正值
    EXHIGH_OFFSET = 50,     //极高差速值  正值

}Speed;

#define MOTOR_LEFT_1 26      //注意此处插线    电机驱动上 PWM1-> MOTOR_X_2  PWM2->MOTOR_X_1
#define MOTOR_LEFT_2 27
#define MOTOR_RIGHT_1 28
#define MOTOR_RIGHT_2 29

void Motor_Init(void);

void Forward(Speed pwm); 
void Backward(Speed pwm);
void turnLeft_FW(Speed pwm,Speed offset);
void turnRight_FW(Speed pwm,Speed offset);
void turnLeft_BW(Speed pwm,Speed offset);
void turnRight_BW(Speed pwm,Speed offset);
void turnRound_R(Speed pwm);
void turnRound_L(Speed pwm);
void brack();

/**
 * @brief 设置左右轮转速 正值为向前转 负值为向后转
 * @param motor_left_pwm 左侧电机的转速
 * @param motor_right_pwm 右侧电机的转速
 * @return void
 * */
inline void Motor_SetPWM(int16_t motor_left_pwm,int16_t motor_right_pwm)
{
    if (motor_left_pwm<0 && motor_left_pwm>=-240)
    {
        analogWrite(MOTOR_LEFT_2,-motor_left_pwm);
        analogWrite(MOTOR_LEFT_1,STOP);
    }    
    else if (motor_left_pwm>=0 && motor_left_pwm<=240)
    {   
        analogWrite(MOTOR_LEFT_1,motor_left_pwm);
        analogWrite(MOTOR_LEFT_2,STOP);
    }
    else 
    {    
        Serial.println("请检查电机的左侧速度值");
        while(1);
    }

    if (motor_right_pwm<0 && motor_right_pwm>=-240)
    {
        analogWrite(MOTOR_RIGHT_2,-motor_right_pwm);
        analogWrite(MOTOR_RIGHT_1,STOP);
    }  
    else if (motor_right_pwm>=0 && motor_right_pwm<=240)
    {   
        analogWrite(MOTOR_RIGHT_1,motor_right_pwm);
        analogWrite(MOTOR_RIGHT_2,STOP);
    }
    else 
    {    
        Serial.println("请检查电机的右侧速度值");
        while(1);
    }
}

#endif
